<template>
  <div class="app-container">
    <el-table
      v-loading="listLoading"
      :data="list"
      style="width: 100%"
      row-key="id"
      border
    >
      <el-table-column
        prop="name"
        label="控制说明"
      />
      <el-table-column
        prop="attr"
        label="属性"
      />
      <el-table-column
        prop="val"
        label="值"
      />
      <el-table-column label="操作" width="140" align="center">
        <template slot-scope="{row}">
          <el-switch
            v-model="row.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value="Y"
            inactive-value="N"
            @change="saveFlowNodeParams(row)"
          />
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import { DeleteFlowParams, GetFlowNodeParamsList, SaveFlowNodeParams, DeleteFlowNodeParams } from '@/api/SystemManagerApi'

const optionsMap = {
  'START': '开始节点',
  'NODE': '审批节点',
  'END': '结束节点'
}

export default {
  filters: {
    exchange(val) {
      return optionsMap[val]
    }
  },
  props: ['flowId', 'nodeId'],
  data() {
    return {
      rules: {
        attr: [{ required: true, message: 'type is required', trigger: 'change' }],
        name: [{ required: true, message: 'type is required', trigger: 'change' }],
        val: [{ required: true, message: 'type is required', trigger: 'change' }]
      },
      title: '编辑控制属性',
      fromTemp: {
        id: null,
        name: null,
        attr: null,
        val: null
      },
      dialogFormVisible: false,
      listLoading: false,
      list: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      GetFlowNodeParamsList(this.flowId, this.nodeId).then(response => {
        this.list = response.data
        this.listLoading = false
      })
    },
    resetFormData() {
      this.fromTemp = {
        id: null,
        name: null,
        flowId: this.flowId,
        attr: null,
        val: null
      }
      this.dialogFormVisible = true
    },
    deleteFlowParams(flowId) {
      this.$confirm('确认要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        DeleteFlowParams(flowId).then(() => {
          this.getList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    saveFlowNodeParams(row) {
      this.listLoading = true
      if (row.status === 'Y') {
        SaveFlowNodeParams({
          nodeId: this.nodeId, paramsId: row.id
        }).then((response) => {
          this.dialogFormVisible = false
          this.getList()
        })
      } else {
        DeleteFlowNodeParams(this.nodeId, row.id).then((response) => {
          this.dialogFormVisible = false
          this.getList()
        })
      }
    }
  }
}
</script>
